package com.yunfeisoft.business.service.impl;

import com.applet.base.BaseServiceImpl;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.PmsProductAttributeCategoryDao;
import com.yunfeisoft.business.dao.inter.PmsProductAttributeDao;
import com.yunfeisoft.business.model.PmsProductAttribute;
import com.yunfeisoft.business.model.PmsProductAttributeCategory;
import com.yunfeisoft.business.service.inter.PmsProductAttributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductAttributeServiceImpl
 * Description: 商品属性表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("pmsProductAttributeService")
public class PmsProductAttributeServiceImpl extends BaseServiceImpl<PmsProductAttribute, String, PmsProductAttributeDao> implements PmsProductAttributeService {

    @Autowired
    private PmsProductAttributeCategoryDao pmsProductAttributeCategoryDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<PmsProductAttribute> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public List<PmsProductAttribute> queryList(Map<String, Object> params) {
        return getDao().queryList(params);
    }

    @Override
    public int addValue(String id, String value) {
        PmsProductAttribute attribute = load(id);
        if (attribute == null) {
            return 0;
        }

        String inputList = attribute.getInputList();
        if (StringUtils.isBlank(inputList)) {
            attribute.setInputList(value);
        } else {
            attribute.setInputList(inputList + "," + value);
        }
        return getDao().update(attribute);
    }

    @Override
    public int save(PmsProductAttribute pmsProductAttribute) {
        int num = getDao().queryNum(pmsProductAttribute.getProductAttributeCategoryId(), pmsProductAttribute.getType());

        PmsProductAttributeCategory category = new PmsProductAttributeCategory();
        category.setId(pmsProductAttribute.getProductAttributeCategoryId());
        if (pmsProductAttribute.getType() == PmsProductAttribute.ProductAttributeTypeEnum.STANDARD.getValue()) {
            category.setAttributeCount(num + 1);
        } else {
            category.setParamCount(num + 1);
        }
        pmsProductAttributeCategoryDao.update(category);

        return super.save(pmsProductAttribute);
    }

    @Override
    public int remove(String id) {
        PmsProductAttribute pmsProductAttribute = getDao().load(id);

        if (pmsProductAttribute != null) {
            int num = getDao().queryNum(pmsProductAttribute.getProductAttributeCategoryId(), pmsProductAttribute.getType());

            PmsProductAttributeCategory category = new PmsProductAttributeCategory();
            category.setId(pmsProductAttribute.getProductAttributeCategoryId());
            if (pmsProductAttribute.getType() == PmsProductAttribute.ProductAttributeTypeEnum.STANDARD.getValue()) {
                category.setAttributeCount(num - 1);
            } else {
                category.setParamCount(num - 1);
            }
            pmsProductAttributeCategoryDao.update(category);
        }
        return super.remove(id);
    }

    @Override
    public int remove(String[] ids) {
        for (String id : ids) {
            remove(id);
        }
        return ids.length;
    }
}